package com.hzcx.app.simplechat.ui.setting;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.setting.adapter.SettingFindMyWayAdapter;
import com.hzcx.app.simplechat.ui.setting.bean.SettingFindMyWayBean;
import com.hzcx.app.simplechat.ui.setting.contract.SettingFindMyWayContract;
import com.hzcx.app.simplechat.ui.setting.presenter.SettingFindMyWayPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 查找我的方式页面
 */
public class SettingFindMyWayActivity extends BaseActivity<SettingFindMyWayPresenter> implements SettingFindMyWayContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rv_my_way)
    RecyclerView rvMyWay;

    private List<SettingFindMyWayBean> listData;
    private SettingFindMyWayAdapter findMyWayAdapter;
    private boolean isSwitchReady = true;

    @Override
    public int getLayout() {
        return R.layout.activity_find_my_way;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SettingFindMyWayPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("添加我的方式");

        listData = new ArrayList<>();
        findMyWayAdapter = new SettingFindMyWayAdapter(listData);
        rvMyWay.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvMyWay.setAdapter(findMyWayAdapter);
    }

    @Override
    public void initData() {
        mPresenter.getSettingFindMyWayList(this);

        findMyWayAdapter.setOnFindMyWaySwitchListener(pos -> {
            if (!isSwitchReady) {
                return;
            }
            isSwitchReady = false;
            mPresenter.updateMyWay(SettingFindMyWayActivity.this, listData.get(pos).getList().getType(), pos);
        });
    }

    /**
     * 列表数据返回成功
     *
     * @param data
     */
    @Override
    public void myWayResult(List<SettingFindMyWayBean> data) {
        try {
            listData.clear();
            listData.addAll(data);
            findMyWayAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改成功
     *
     * @param pos
     */
    @Override
    public void updateSuccess(int pos) {
        if (listData.get(pos).getList().getWay_sate() == 0) {
            listData.get(pos).getList().setWay_sate(1);
        } else {
            listData.get(pos).getList().setWay_sate(0);
        }
        isSwitchReady = true;
    }

    /**
     * 修改失败
     *
     * @param pos
     */
    @Override
    public void updateFail(int pos) {
        findMyWayAdapter.notifyDataSetChanged();
        isSwitchReady = true;
    }

}
